package com.sky.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import com.sky.service.SetmealDishService;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    //增加套餐
    @PostMapping
    @ApiOperation(value = "新增套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDto.categoryId")
    public Result<String> save(@RequestBody SetmealDTO setmealDto){
        setmealService.saveWithDish(setmealDto);
        return Result.success("新增套餐成功");
    }
    //套餐的分页查询
    @GetMapping("/page")
    @ApiOperation(value = "套餐的分页查询")
    public Result<Page> page(SetmealPageQueryDTO setmealPageQueryDTO){
        Page<SetmealVO> page = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(page);
    }
    //根据id查询套餐
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询套餐")
    public Result<SetmealVO> getSetmealById(@PathVariable Long id){
        SetmealVO setmealVO = setmealService.getSetmealWithDish(id);
        return Result.success(setmealVO);
    }
    //修改套餐
    @PutMapping
    @ApiOperation(value = "修改套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result updateWithDish(@RequestBody SetmealDTO setmealDTO){
        setmealService.updateWithDish(setmealDTO);
        return Result.success("套餐修改成功");
    }
    //停售（起售）套餐
    @PostMapping("/status/{status}")
    @ApiOperation(value = "停售（起售）套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result<String> updateStatus(@PathVariable int status,Long id){
        setmealService.updateStatus(status,id);
        return Result.success("套餐状态修改成功");
    }
    //批量删除套餐
    @DeleteMapping
    @ApiOperation(value = "批量删除套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result<String> delete(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);
        return Result.success("套餐删除成功");
    }

}
